package com.scpii.mobi.page.action.flex;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ServiceException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.annotations.SMDMethod;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import cn.com.WebXml.IpAddressSearchWebServiceLocator;
import cn.com.WebXml.IpAddressSearchWebServiceSoap;

import com.scpii.mobi.page.common.base.BaseAction;
import com.scpii.mobi.page.domain.Result;

import freemarker.template.Template;

public class SubAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1790446973894408684L;

	@Resource
	private JavaMailSender mailSender;
	@Resource
	private FreeMarkerConfigurer freeMarkerConfigurer;

	private static final String MAIL_TO = "lx@gotoapp.cn";

	private static final String[] MAIL_CC = new String[] { "xzy@gotoapp.cn",
			"zlb@gotoapp.cn" };

	@SMDMethod
	public Boolean execute(List<Result> results) throws JSONException,
			MessagingException, RemoteException, ServiceException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getHeader("X-Real-IP");
		sendMail(results, ip);
		return true;
	}

	private void sendMail(List<Result> results, String ip)
			throws MessagingException, RemoteException, ServiceException {
		String[] citys = getCity(ip);
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, false,
				"UTF-8");
		helper.setText(getMailText(results), true);
		helper.setFrom("xzy@gotoapp.cn");
		helper.setSentDate(new Date());
		helper.setSubject("网速测试报告  IP地址：" + citys[0] + " 城市: " + citys[1]);
		helper.setTo(MAIL_TO);
		helper.setCc(MAIL_CC);
		mailSender.send(message);

	}

	private String[] getCity(String ip) throws RemoteException,
			ServiceException {
		IpAddressSearchWebServiceLocator locator = new IpAddressSearchWebServiceLocator();
		IpAddressSearchWebServiceSoap soap = locator
				.getIpAddressSearchWebServiceSoap();
		return soap.getCountryCityByIp(ip);
	}

	private String getMailText(List<Result> results) {
		String htmlText = "";
		try {
			Template template = freeMarkerConfigurer.getConfiguration()
					.getTemplate("mail.ftl");
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("results", results);
			htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(
					template, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return htmlText;
	}
}
